<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/4/30
  Time: 10:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../common.jsp"%>
<html>
<head>
    <title>任务列表</title>
</head>
<body>

<table id="jobGrid" class="easyui-datagrid" title="任务列表" style="width:100%;height:100%" toolbar="#tb">
    <thead>
    <tr>
        <th field="jobName" width="150">任务名称</th>
        <th field="jobGroup" width="150">任务分组</th>
        <th field="description" width="150" align="right">描述</th>
        <th field="jobClassName" width="150" align="right">执行类</th>
        <th field="cronExpression" width="150" align="right">执行时间</th>
    </tr>
    </thead>
</table>
<div id="tb">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addJob()">新增任务</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editJob()">修改任务</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="delJob()">删除任务</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="pauseJob()">暂停任务</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="triggerJob()">触发任务</a>
</div>

<div id="job_edit" class="easyui-dialog" title="添加任务" data-options="iconCls:'icon-save'" style="border: 0; width: 500px;">
    <form id="mainForm" method="post">
        <table cellpadding="5">
            <tr>
                <td>任务名称:</td>
                <td><input class="easyui-textbox" type="text" name="jobName" data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>任务分组:</td>
                <td><input class="easyui-textbox" type="text" name="jobGroup" data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>描述:</td>
                <td><input class="easyui-textbox" type="text" name="description" data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>执行类:</td>
                <td><input class="easyui-textbox" type="text" name="jobClassName" data-options="required:true"></input></td>
            </tr>
            <tr>
                <td>执行时间:</td>
                <td>
                    <input class="easyui-textbox" type="text" name="cronExpression" data-options="required:true"></input>
                </td>
            </tr>
        </table>
    </form>
    <div style="text-align: center; padding: 5px">
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm('add')" id="addSub">添加</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm('update')" id="editSub">修改</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeForm()">取消</a>
    </div>

</div>
<script type="application/javascript">
   $(function(){
       $("#job_edit").dialog('close');
       $("#jobGrid").datagrid({
           url:ctx+"/job/jobList",
           pagination:true,
           pageSize:20,
           pageList:[20,50,100],
           singleSelect:true,
           rownumbers:true,
           checkOnSelect:true,
           onBeforeLoad : function(param) {
               $.messager.progress({
                   text : '数据加载中....'
               });
           },
           onLoadSuccess : function(data) {
               $.messager.progress('close');
           },
           onLoadError : function() {
               $.messager.progress('close');
           }
       });

   });

   function addJob(){
       $("#job_edit").dialog('open');
       $("#editSub").hide();
       $("#addSub").show();
   }

   function editJob(){
       var rows = $("#jobGrid").datagrid("getSelected");
       if(rows){
           $("#job_edit").dialog('open');
           $("#editSub").show();
           $("#addSub").hide();
       }else{
           $.messager.alert("操作提示", "请先选中一行！", "error");
       }
   }

   function delJob(){
       var rows = $("#jobGrid").datagrid("getSelected");
       if(rows){
           $.ajax({
               url : ctx+'/job/removeJob',
               data : {
                   jobName : rows.jobName,
                   jobGroup: rows.jobGroup
               },
               type : "GET",
               success : function(data) {
                   if (data.status == true) {
                       // 重新加载
                       $('#jobGrid').datagrid('reload');
                       $.messager.confirm("操作提示",data.respMsg,"info");
                   } else {
                       $.messager.alert("操作提示", data.respMsg, "error");
                   }
               },
               error : function() {
                   $.messager.alert("操作提示", "操作失败！", "error");
               }
           });
       }else{
           $.messager.alert("操作提示", "请先选中一行！", "error");
       }
   }

   function pauseJob(){
       var rows = $("#jobGrid").datagrid("getSelected");
       if(rows){
           $.ajax({
               url : ctx+'/job/pauseJob',
               data : {
                   jobName : rows.jobName,
                   jobGroup: rows.jobGroup
               },
               type : "GET",
               success : function(data) {
                   if (data.status == true) {
                       // 重新加载
                       $('#jobGrid').datagrid('reload');
                       $.messager.confirm("操作提示",data.respMsg,"info");
                   } else {
                       $.messager.alert("操作提示", data.respMsg, "error");
                   }
               },
               error : function() {
                   $.messager.alert("操作提示", "操作失败！", "error");
               }
           });
       }else{
           $.messager.alert("操作提示", "请先选中一行！", "error");
       }
   }

   function triggerJob(){
       var rows = $("#jobGrid").datagrid("getSelected");
       if(rows){
           $.ajax({
               url : ctx+'/job/triggerJob',
               data : {
                   jobName : rows.jobName,
                   jobGroup: rows.jobGroup
               },
               type : "GET",
               success : function(data) {
                   if (data.status == true) {
                       // 重新加载
                       $('#jobGrid').datagrid('reload');
                       $.messager.confirm("操作提示",data.respMsg,"info");
                   } else {
                       $.messager.alert("操作提示", data.respMsg, "error");
                   }
               },
               error : function() {
                   $.messager.alert("操作提示", "操作失败！", "error");
               }
           });
       }else{
           $.messager.alert("操作提示", "请先选中一行！", "error");
       }
   }

   function submitForm(data){
        var reqData = $('#mainForm').find('input').not('[value=""]').serializeJSON();
       if(data=='update'){
           var rows = $("#jobGrid").datagrid("getSelected");
          reqData.oldJobName=rows.jobName;
          reqData.oldJobGroup=rows.jobGroup
       }
       $.ajax({
           url : ctx+'/job/addJob',
           data :reqData,
           type : "POST",
           success : function(data) {
               if (data.status == true) {
                   // 重新加载
                   $('#jobGrid').datagrid('reload');
                   $.messager.confirm("操作提示",data.respMsg,"info");
               } else {
                   $.messager.alert("操作提示", data.respMsg, "error");
               }
           },
           error : function() {
               $.messager.alert("操作提示", "操作失败！", "error");
           }
       });

   }
   function closeForm(){
       $("#job_edit").dialog('close');
   }
</script>
</body>
</html>
